<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);
session_start();
$startDate = $_SESSION["startDate"];
$endDate = $_SESSION["endDate"];
$startDate1 = date('dmy', strtotime($startDate));
$endDate1 = date('dmy', strtotime($endDate));
$userName = $_SESSION['loginusername'];

if (!empty($_SESSION["userType"])) {
    $userType = $_SESSION["userType"];
}

$sql = "Select Email from user where UserType='Centre Manager' AND Username = '$userName'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $email = $row['Email'];
}

date_default_timezone_set('Asia/Singapore');

require_once('PHPMailer/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail = new PHPMailer();

$body = file_get_contents('../Reportsemail.html');
$body = eregi_replace("[\]", '', $body);
//include 'conn.php';
//$sql = "Select Email,StaffName from user,centre where Email IS NOT NULL AND centre.CentreName=".$centreName." AND user.CentreId=centre.CentreId";
//$result = mysql_query($sql);
//$row = mysql_fetch_array($result);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2;                     // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port = 465;                   // set the SMTP port for the GMAIL server
$mail->Username = "fp.neth@gmail.com";  // GMAIL username
$mail->Password = "howdchanoe?";            // GMAIL password

$mail->SetFrom('fp.neth@gmail.com', 'Eldercare Reports');

$mail->AddReplyTo($email, "Centre Manager");

$mail->Subject = "Eldercare Reports from " . $startDate . " to " . $endDate . " of " . $_SESSION["centreName"] . " Centre";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->Priority = 1;
//$address = $row['Email'];
$mail->AddAddress($email, "Centre Manager");
$attachment = "../Attendance/EldercareReport" . $startDate1 . "to" . $endDate1 . "-" . $_SESSION["centreName"] . ".xlsx";
$mail->AddAttachment($attachment);      // attachment
$mail->AddEmbeddedImage('../images/eldercare.gif', 'logo_2u');

if (!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    session_start();
    $_SESSION["sent"] = true;
}
?>